<?php
/**
 * Created by PhpStorm.
 * User: apple
 * Date: 2020/10/12
 * Time: 4:06 PM
 */
namespace app\admin\logic;

use app\model\Leave;
use app\model\User;
use think\facade\Db;
use think\facade\Request;

class LeaveLogic
{

    public function getList()
    {
        $param = Request::get();

        $where = [];
        if(isset($param['pername']) || !empty($param['pername'])){
            $where[] = ['pername','like','%'.trim($param['pername']).'%'];
        }
        if(isset($param['type']) || !empty($param['type'])){
            $where[] = ['leave_type','=',intval($param['type'])];
        }
        $where[] = ['leave_status','=',1];    //默认是 没删除的
        $where[] = ['leave_orgid','=',config('user')['orgid']];
        $leaveModel = new Leave();
        $list = $leaveModel->getLeaveListByCondition($where);
        return $list;
    }

    public function pass()
    {
        $param = Request::post();

        if(!isset($param['leaveId']) || empty($param['leaveId'])) fail('请选择请假记录');
        $leaveId = intval($param['leaveId']);
        if(!isset($param['approval']) || empty($param['approval'])) fail('请选择通过或者拒绝');
        $approval = intval($param['approval']);

        $reason = '';
        if($approval == Leave::APPROVAL_REJECT){
            if(!isset($param['reason']) || empty($param['reason'])) fail('请输入拒绝理由');
            $reason = trim($param['reason']);
        }
        $leaveModel = new Leave();
        $leaveInfo = $leaveModel->getLeaveInfoById($leaveId);

        if(!$leaveInfo)   fail('请假信息不存在');

        if($leaveInfo['approval'] == Leave::APPROVAL_WITHDRAW)    fail('请假已撤回');
        if($leaveInfo['approval'] == Leave::APPROVAL_PASS)    fail('请假已通过');
        if($leaveInfo['approval'] == Leave::APPROVAL_REJECT)    fail('请假已拒绝');

        $leave = [
            'leave_approval' => $approval,
            'leave_approval_time' => time(),
            'leave_reason' => $reason,
            'leave_approval_uid' => config('user')['id'],
            'leave_approval_name' => config('user')['pername'],
        ];

        $rt = $leaveModel->updateLeaveByWhere(['leave_id'=>$leaveId],$leave);
        if(!$rt)    fail('操作失败，请稍后重试');

        return true;
    }

    public function download()
    {
        $param = Request::get();

        $where = [];
        if(isset($param['pername']) || !empty($param['pername'])){
            $where[] = ['pername','like','%'.trim($param['pername']).'%'];
        }
        if(isset($param['type']) || !empty($param['type'])){
            $where[] = ['leave_type','=',intval($param['type'])];
        }
        $where[] = ['leave_status','=',1];    //默认是 没删除的
        $where[] = ['leave_orgid','=',config('user')['orgid']];
        $leaveModel = new Leave();
        $list = $leaveModel->getLeaveListByWhere($where);
        return $list;
    }
}